import { createStore } from 'vuex'
import persist from 'vuex-persistedstate';
export default createStore({
  state: {
    carList:[], // 购物车的数据
  },
  mutations: {
    storeCart(state,val){ // 加入购物车
      // console.log(val);
      let i = state.carList.findIndex(item => item.tiesId == val.tiesId);
      if (i>-1){
        state.carList[i].num += val.num;
      } else{
        state.carList.push(val);
      }
    },
    storeCheck(state,val){ // 全选的事件
      state.carList.forEach(item => {
        item.ischeck = val
      })
    },
    storeDel(state,i){ // 单个删除
      state.carList.splice(i,1);
    },
    deleteAll(state){ // 批量删除(
      state.carList = state.carList.filter(item => item.ischeck == false);
    }

  },
  actions: {
  },
  modules: {
  },
  plugins: [
    new persist({
      storage: window.localStorage,
    }),
  ]
})
